package cc.service.impl;

import cc.dao.UserMapper;
import cc.entity.User;
import cc.entity.UserPasswordEdit;
import cc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl  implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int save(User user) throws Exception{
        return userMapper.add(user);
    }
    @Override
    public User getById(Integer id)  throws Exception{
        return userMapper.getById(id);
    }

    @Override
    public User getById2(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User update(UserPasswordEdit userPassowrdEdit) {
        User user = userMapper.getById(userPassowrdEdit.getId());
        if(user != null){
            if(user.getPassword().equals(userPassowrdEdit.getOldPassword())){
                if(userPassowrdEdit.getOldPassword().equals(userPassowrdEdit.getNewPassword())){
                    return null;
                }else {
                    // 旧密码验证正确，新旧密码不同，可以更新
                    user.setPassword(userPassowrdEdit.getNewPassword());
                }
            }else {
                return null;
            }
        }
        userMapper.updatePassword(user);

        return user;
    }

}
